package com.example.smallwork;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

public class Login extends AppCompatActivity {
    private String userName, psw, spPsw;
    private EditText user, password;//引入EditText控件
    final int TIME = 3;    //定义时间长度
    final int MSG_CODE = 0x001;    //定义消息代码
    private ProgressBar progressBar;    //声明水平进度条
    private int mProgressStatus = 0;    //定义完成进度
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login2);
        user = findViewById(R.id.user);
        password = findViewById(R.id.password);
        Button b1 = (Button) findViewById(R.id.b1);
//        Toast.makeText(Login.this,"1111",Toast.LENGTH_SHORT).show();
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Toast.makeText(context,"1111",Toast.LENGTH_SHORT).show();
                getText();
//                Toast.makeText(context,"22222",Toast.LENGTH_SHORT).show();
                spPsw = readPsw(userName);
                if (TextUtils.isEmpty(userName)) {
                    Toast.makeText(Login.this, "请输入用户名", Toast.LENGTH_SHORT).show();
                    return;
                } else if (TextUtils.isEmpty(psw)) {
                    Toast.makeText(Login.this, "请输入密码", Toast.LENGTH_SHORT).show();
                    return;
                } else if (spPsw.equals(psw)) {
                    Toast.makeText(Login.this, "登录成功", Toast.LENGTH_SHORT).show();
                    progressBar = findViewById(R.id.timer);      //获取进度条组件
                    handler.sendEmptyMessage(MSG_CODE);
//                    Toast.makeText(Login.this,"1111",Toast.LENGTH_SHORT).show();
//                    startActivity(new Intent(Login.this, MainActivity.class));
                    return;
                } else if ((spPsw != null && !TextUtils.isEmpty(spPsw) && !psw.equals(spPsw))) {
                    Toast.makeText(Login.this, "输入的用户名和密码不一致", Toast.LENGTH_SHORT).show();
                    return;
                } else {
                    Toast.makeText(Login.this, "此用户名不存在", Toast.LENGTH_SHORT).show();
                }
            }
        });
        Button b2 = (Button) findViewById(R.id.b2);
        b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Login.this, registerActivity.class);//从登录页面跳转到注册页面
                startActivity(intent);
            }
        });
    }
    private void getText() {
        userName = user.getText().toString().trim();
        psw = password.getText().toString().trim();
    }

    private String readPsw(String userName) {
        SharedPreferences sp = getSharedPreferences("loginInfo", MODE_PRIVATE);
        return sp.getString(userName, "");
    }


    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        super.onActivityResult(requestCode, resultCode, data);
        if (data != null) {
            //是获取注册界面回传过来的用户名
            String userName = data.getStringExtra("userName");
            if (!TextUtils.isEmpty(userName)) {

            }
        }
    }
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            //当前进度大于0
            if (TIME - mProgressStatus > 0) {
                mProgressStatus++;         //进度+1
                progressBar.setProgress(mProgressStatus);            //更新进度条的显示进度
                handler.sendEmptyMessageDelayed(MSG_CODE, 1000);    //延迟一秒发送消息
            } else {
                //提示时间已到
                Intent intent = new Intent(Login.this, MainActivity.class);
                intent.putExtra("user", userName);
                startActivity(intent);
            }
        }
    };
}